//
//  STRotationViewController.m
//  BasicAnimation-OC
//
//  Created by Stan on 2017-02-10.
//  Copyright © 2017 stan. All rights reserved.
//

#import "STRotationViewController.h"

@interface STRotationViewController ()
@property(weak,nonatomic)CALayer * redLayer;
@property(weak,nonatomic)CALayer * blueLayer;
@property(weak,nonatomic)CALayer * grayLayer;
@property(weak,nonatomic)CALayer * greenLayer;

@property(weak,nonatomic)CALayer *redRotationLayer;
@property(weak,nonatomic)CALayer *blueRotationLayer;
@property(weak,nonatomic)CALayer *grayRotationLayer;
@property(weak,nonatomic)CALayer *greenRotationLayer;
@end

@implementation STRotationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    创建红色x轴缩放的Layer
    self.redLayer = [self createLayerWithPosition:CGPointMake(125, 150) backgroundColor:[UIColor redColor] tilteName:@"x轴缩放"];
    [self.redLayer addAnimation:[self createBasicAnimationWithKeyPath:@"transform.scale.x" toValue:0.5] forKey:nil];
    
    //    创建蓝色y轴缩放的Layer
    self.blueLayer = [self createLayerWithPosition:CGPointMake(125, 250) backgroundColor:[UIColor blueColor] tilteName:@"y轴缩放"];
    [self.blueLayer addAnimation:[self createBasicAnimationWithKeyPath:@"transform.scale.y" toValue:0.5] forKey:nil];
   
    //    创建灰色z轴缩放的Layer
    self.grayLayer = [self createLayerWithPosition:CGPointMake(125, 350) backgroundColor:[UIColor grayColor] tilteName:@"z轴缩放"];
    [self.grayLayer addAnimation:[self createBasicAnimationWithKeyPath:@"transform.scale.z" toValue:0.5] forKey:nil];
    
    //    创建绿色xyz轴同时缩放Layer
    self.greenLayer = [self createLayerWithPosition:CGPointMake(125, 450) backgroundColor:[UIColor greenColor] tilteName:@"xyz轴缩放"];
    [self.greenLayer addAnimation:[self createBasicAnimationWithKeyPath:@"transform.scale" toValue:0.5] forKey:nil];
    
    
//    旋转
    self.redRotationLayer = [self createLayerWithPosition:CGPointMake(250, 150) backgroundColor:[UIColor redColor] tilteName:@"x轴旋转"];
    [self.redRotationLayer addAnimation:[self createBasicAnimationWithKeyPath:@"transform.rotation.x" toValue:M_PI_4] forKey:nil];
    
    //    创建蓝色y轴缩放的Layer
    self.blueRotationLayer = [self createLayerWithPosition:CGPointMake(250, 250) backgroundColor:[UIColor blueColor] tilteName:@"y轴旋转"];
        [self.blueRotationLayer addAnimation:[self createBasicAnimationWithKeyPath:@"transform.rotation.y" toValue:M_PI_4] forKey:nil];
    
    //    创建灰色z轴缩放的Layer
    self.grayRotationLayer = [self createLayerWithPosition:CGPointMake(250, 350) backgroundColor:[UIColor grayColor] tilteName:@"z轴旋转"];
        [self.grayRotationLayer addAnimation:[self createBasicAnimationWithKeyPath:@"transform.rotation.z" toValue:M_PI_4] forKey:nil];
    

 

    
    
//    添加几个Label，为了让演示的时候看上去更加清晰
    [self createUILabelWithTitle:@"缩放" frame:CGRectMake(100, 80, 50, 20)];
    [self createUILabelWithTitle:@"旋转" frame:CGRectMake(250, 80, 50, 20)];
    [self createUILabelWithTitle:@"X轴" frame:CGRectMake(20, 125, 50, 20)];
    [self createUILabelWithTitle:@"Y轴" frame:CGRectMake(20, 225, 50, 20)];
    [self createUILabelWithTitle:@"Z轴" frame:CGRectMake(20, 325, 50, 20)];
    [self createUILabelWithTitle:@"XYZ轴" frame:CGRectMake(20, 425, 50, 20)];


}


//创建CALayer
- (CALayer *)createLayerWithPosition:(CGPoint)position backgroundColor:(UIColor *)backgroundColor tilteName:(NSString *)title{
    //创建calayer
    CALayer * layer = [CALayer layer];
    
    //设置位置和大小
    layer.position = position;
    layer.bounds= CGRectMake(0, 0, 100, 100);
    
    //设置背景颜色
    layer.backgroundColor = backgroundColor.CGColor;
    
    //把layer添加到UIView的layer上
    [self.view.layer addSublayer:layer];
    
    
    return layer;
}

//创建缩放动画
- (CABasicAnimation *)createBasicAnimationWithKeyPath:(NSString *)keyPath toValue:(CGFloat)toValue{
    //创建动画对象
    CABasicAnimation *scaleAni = [CABasicAnimation animation];
    
    //设置动画属性
    scaleAni.keyPath = keyPath;
    
    //设置动画的起始位置。也就是动画从哪里到哪里。不指定起点，默认就从positoin开始
    scaleAni.toValue = @(toValue);
    
    //动画持续时间
    scaleAni.duration = 2;
    
    //动画重复次数
    scaleAni.repeatCount = CGFLOAT_MAX;
    
    return scaleAni;
}



//创建Label
- (void)createUILabelWithTitle:(NSString *)title frame:(CGRect)frame{
    UILabel *label = [[UILabel alloc]init];
    label.text = title;
    label.frame = frame;
    label.textColor = [UIColor darkGrayColor];
    label.font = [UIFont systemFontOfSize:12];
    
    [self.view addSubview:label];
    
}


@end
